package com.muyaxuan.mail.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.muyaxuan.mail.dto.User;

public class UserDao extends BaseDao{
	public static final String NAME_SPACE="com.muyaxuan.mail.dto.User.";
	User user = null;
	/**
	 * 根据名字查询用户的DAO方法
	 * @param user
	 * @return
	 */
	public User getUserByName(String name){
		//1  获取连接
		SqlSession session = getSqlSession();
		//2
		user = new User();
		user.setName(name);
		
		User u=session.selectOne(NAME_SPACE+"getUserByName",user);
		//返回之前关闭会话
		closeSqlSession();
		
		return u;
		
	}
	/**
	 * 根据密码和名字查询用户的DAO方法
	 * @param user
	 * @return
	 */
	public User getUserByUserIdAndPass(User user){
		//1  获取连接
		SqlSession session = getSqlSession();
		//2
		User u=session.selectOne(NAME_SPACE+"getUserByUserIdAndPass",user);
		//返回之前关闭会话
		closeSqlSession();
		
		return u;
		
	}
	/**
	 * 获取管理员方法
	 * @param user
	 * @return
	 */
	public User getAdmin(User user){
		
		SqlSession session=getSqlSession();
		
		User u = session.selectOne(NAME_SPACE+"getAdmin",user);
		
		closeSqlSession();
		
		return u;
		
	}
	
	public List<User> getUserByPage(Map<String,Object> params){
		
		//List<User> userList = new ArrayList<>();
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		List<User> userList = session.selectList(NAME_SPACE+"getUserByPage",params);
		//返回集合
		return userList;
	}
    //	查询用户数量
	public int count(String userId){
		
		SqlSession session = getSqlSession();
		
        User user = new User();
		
		user.setUserId(userId);
		
		int count = session.selectOne(NAME_SPACE+"count",user);
		
		return count;
	}
    //	用户注册
	public String insertUser(User user){
		
		SqlSession session = getSqlSession();
		
		int count = session.update(NAME_SPACE+"save",user);
		
		session.commit();
		
		closeSqlSession();
		
		return count>0?"注册成功":"注册失败";
	}
	/**
	 * 根据id查询用户方法
	 */
	public User getUserById(int id){
		
		User user = getSqlSession().selectOne(NAME_SPACE+"getUserById", id);
		
		closeSqlSession();
		
		return user;
	}

}
